package com.cyrus.vueboot.controller.dpt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyrus.vueboot.common.R;
import com.cyrus.vueboot.error.BusinessError;
import com.cyrus.vueboot.error.CommonError;
import com.cyrus.vueboot.pojo.dpt.DepartmentUser;
import com.cyrus.vueboot.service.DepartmentUserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/25
 */
@RestController
@Slf4j
@Api
@RequestMapping("/departmentUser")
public class DepartmentUserController {
    @Autowired
    DepartmentUserService departmentUserService;

    @PostMapping("/search")
    public R search(Integer page, Integer size, String key) {
        IPage iPage=departmentUserService.search(page, size, key);
        List records = iPage.getRecords();
        long total = iPage.getTotal();
        long size1 = iPage.getSize();
        return R.ok().data("records", records).data("total", total).data("size",size1);
    }
    @PostMapping("/getAllDptUser")
    public R getAllDptUser(String dptName) {
        List<DepartmentUser> list = departmentUserService.list(new QueryWrapper<DepartmentUser>().lambda().eq(DepartmentUser::getDptName, dptName));
        return R.ok().data("list", list);
    }


    @PostMapping("/delDepartmentUsers")
    public R delDepartmentUsers(Long[] ids) {
        try {
            departmentUserService.delDepartmentUsers(ids);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10002.getCode(), CommonError.E_10002.getMsg());
        }
    }

    @PostMapping("/findById")
    public R findById(Integer id) {

        DepartmentUser user = departmentUserService.getById(id);
        return R.ok().data("user", user);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody DepartmentUser departmentUser) {
        try {
            departmentUserService.updateById(departmentUser);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10003.getCode(), CommonError.E_10003.getMsg());
        }

    }

    @PostMapping("/add")
    public R add(@RequestBody DepartmentUser departmentUser) {
        try {
            departmentUser.setIsDelete(0);
            departmentUserService.save(departmentUser);
            return R.ok();
        } catch (Exception e) {
            throw new BusinessError(CommonError.E_10001.getCode(), CommonError.E_10001.getMsg());
        }
    }
}
